package com.lakeworks.service.impl;

import com.lakeworks.common.core.text.Convert;
import com.lakeworks.domain.SysProduct;
import com.lakeworks.mapper.SysProductMapper;
import com.lakeworks.service.ISysProductService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysProductServiceImpl implements ISysProductService {

    @Resource
    private SysProductMapper sysProductMapper;

    @Override
    @Cacheable(value = "product", key = "#id",unless = "#result == null")
    public SysProduct selectSysProductById(Long id) {
        return sysProductMapper.selectSysProductById(id);
    }

    @Override
    @Cacheable(value = "sysProducts", key = "#channelId",unless = "#result?.size() == 0")
    public List<SysProduct> productListByChannelId(Long channelId) {
        return sysProductMapper.selectSysProductList(new SysProduct(channelId));
    }

    @Override
    public List<SysProduct> selectSysProducts(SysProduct sysProduct) {
        return sysProductMapper.selectSysProductList(sysProduct);
    }

    @Override
    @CacheEvict(value = "sysProducts", allEntries = true)
    public int insertSysProduct(SysProduct sysProduct) {
        return sysProductMapper.insertSysProduct(sysProduct);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = "sysProducts", allEntries = true),
            @CacheEvict(value = "product", key = "#sysProduct.id")
    })
    public int updateSysProduct(SysProduct sysProduct) {
        return sysProductMapper.updateSysProduct(sysProduct);
    }

    @Override
    @CacheEvict(value = "sysProducts", allEntries = true)
    public int deleteSysProductByIds(String ids) {
        return sysProductMapper.deleteSysProductByIds(Convert.toStrArray(ids));
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = "sysProducts", allEntries = true),
            @CacheEvict(value = "product", key = "#id")
    })
    public int deleteSysProductById(Long id) {
        return sysProductMapper.deleteSysProductById(id);
    }
}
